﻿using System;
using System.Net;
using System.Windows;
using Windows.Foundation;
using Windows.UI.Xaml;

namespace BlueRoseGames.Controls.Text
{
    public sealed class SineTransform : Transform
    {
        #region DependencyProperties
        double _amplitude;
        double _startAngle;
        double _period;

        public static readonly DependencyProperty AmplitudeProperty = DependencyProperty.Register("Amplitude", typeof(double), typeof(SineTransform), new PropertyMetadata(0d, OnAmplitudeChanged));
        public double Amplitude
        {
            get { return (double)GetValue(AmplitudeProperty); }
            set { SetValue(AmplitudeProperty, value); }
        }

        public static readonly DependencyProperty PeriodProperty = DependencyProperty.Register("Period", typeof(double), typeof(SineTransform), new PropertyMetadata(0d, OnPeriodChanged));
        public double Period
        {
            get { return (double)GetValue(PeriodProperty); }
            set { SetValue(PeriodProperty, value); }
        }

        public static readonly DependencyProperty StartAngleProperty = DependencyProperty.Register("StartAngle", typeof(double), typeof(SineTransform), new PropertyMetadata(0d, OnStartAngleChanged));
        public double StartAngle
        {
            get { return (double)GetValue(StartAngleProperty); }
            set { SetValue(StartAngleProperty, value); }
        }

        private static void OnAmplitudeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var st = (SineTransform)dependencyObject;
            st._amplitude = (double)e.NewValue;
            st.Changed = true;
        }

        private static void OnStartAngleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var st = (SineTransform)dependencyObject;
            st._startAngle = (double)e.NewValue;
            st.Changed = true;
        }

        private static void OnPeriodChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var st = (SineTransform)dependencyObject;
            st._period = (double)e.NewValue;
            st.Changed = true;
        }        
        #endregion

        public override Point TransformPoint(Point p)
        {
            Point ret = new Point();
            double angle = ((p.X / _period) + (_startAngle / 360d)) * Math.PI * 2;
            ret.X = p.X;
            ret.Y = Math.Sin(angle) * _amplitude + p.Y;
            return ret;
        }
    }
}
